package jmetal.qualityIndicator.util;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

import jmetal.core.Solution;
import jmetal.core.SolutionSet;

public class FileRoW {
	/**
	 * 从文件中读取size个解并返回
	 * @param path
	 * @param size
	 * @return
	 */
	public SolutionSet getPopulationFromFile(String path,int size) {


		SolutionSet population = new SolutionSet(size);

		try {
			// Open the file
			FileInputStream fis = new FileInputStream(path);
			InputStreamReader isr = new InputStreamReader(fis);
			BufferedReader br = new BufferedReader(isr);

			int numberOfObjectives = 0;
			String aux = br.readLine();
			int i = 0;
			while (aux != null&&i<size) {
				StringTokenizer st = new StringTokenizer(aux);
				int j = 0;
				numberOfObjectives = st.countTokens();
				Solution solution = new Solution(numberOfObjectives);
				while (st.hasMoreTokens()) {
					double value = (new Double(st.nextToken())).doubleValue();
					solution.setObjective(j, value);
					
					// System.out.println("lambda["+i+","+j+"] = " + value) ;
					j++;
				}
				population.add(i, solution);
				aux = br.readLine();
				i++;
			}
			br.close();
		} catch (Exception e) {
			System.out
					.println("FileRoW: Failed when reading for file: "
							+ "/" + path);
			e.printStackTrace();
		}

		return population;
	}
}
